package vn.com.vega.music.objects;


import vn.com.vega.music.utils.Util;
import android.os.Parcel;
import android.os.Parcelable;

public class Song implements Parcelable {

	public static final int SONG_TYPE_SERVER = 0;
	public static final int SONG_TYPE_LOCAL = 1;

	public int id = 0;
	public String title = "";
	public int duration = 0;
	public int artist_id = 0;
	public String artist_name = "";
	public int album_id = 0;
	public String album_title = "";
	public String cached_file_path = "";
	public int wait_download = 0;
	public String imageUrl = "";
	public String shareMessage = "";
	public boolean isFavorite = false;
	public String listenPrice;
	public String downloadPrice;
	
	public int genre_id = 0;
	public String genre_name = "";
	
	// chacha
	public int viewCount = 0;

	protected int type;
	
	public Song() {
	}
	
	public Song(int type) {
		this.type = type;
	}

	public Song(Song song) {
		id = song.id;
		title = song.title;
		duration = song.duration;
		artist_id = song.artist_id;
		artist_name = song.artist_name;
		album_id = song.album_id;
		album_title = song.album_title;
		cached_file_path = song.cached_file_path;
		wait_download = song.wait_download;
		type = song.type;
		imageUrl = song.imageUrl;
		genre_id = song.genre_id;
		genre_name = song.genre_name;
		shareMessage = song.shareMessage;
		viewCount = song.viewCount;
	}

	public boolean isSameIdDiffInfo(Song song) {
		if (id != song.id) {
			return false;
		}

		// public int id = 0;
		// public String title = "";
		// public int duration = 0;
		if (!song.title.equals(title) || (song.duration != duration)) {
			return true;
		}

		// public int artist_id = 0;
		// public String artist_name = "";
		if ((song.artist_id != artist_id) || (!song.artist_name.equals(artist_name))) {
			return true;
		}

		// public int album_id = 0;
		// public String album_title = "";
		if ((song.album_id != album_id) || (!song.album_title.equals(album_title))) {
			return true;
		}
		
		if ((song.imageUrl != imageUrl)) {
			return true;
		}

		return false;
	}

	public void updateDiffFrom(Song song) {
		title = song.title;
		artist_id = song.artist_id;
		artist_name = song.artist_name;
		genre_id = song.genre_id;
		genre_name = song.genre_name;
		album_id = song.album_id;
		album_title = song.album_title;
		imageUrl = song.imageUrl;
	}

	public boolean isLocalSong() {
		return (this.type == SONG_TYPE_LOCAL);
	}

	public boolean isAvailableLocally() {
		if ((cached_file_path != null) && (cached_file_path.trim().length() > 0)) {
			return true;
		}
		return false;
	}

	public boolean isWaitToDownload() {
		return !isAvailableLocally() && (wait_download != 0);
	}

	/**
	 *  Implement for pass object via aidl
	 */
	
	public void writeToParcel(Parcel out) {
		out.writeInt(id);
		out.writeString(title);
		out.writeInt(duration);
		out.writeInt(artist_id);
		out.writeString(artist_name);
		out.writeInt(album_id);
		out.writeString(album_title);
		out.writeString(cached_file_path);
		out.writeInt(wait_download);
		out.writeInt(type);
		out.writeString(imageUrl);
		out.writeInt(genre_id);
		out.writeString(genre_name);
		out.writeInt(viewCount);
    }

    public void readFromParcel(Parcel in) {
    	id = in.readInt();
    	title = in.readString();
    	duration = in.readInt();
    	artist_id = in.readInt();
    	artist_name = in.readString();
    	album_id = in.readInt();
    	album_title = in.readString();
    	cached_file_path = in.readString();
    	wait_download = in.readInt();
    	type = in.readInt();
    	imageUrl = in.readString();
    	genre_id= in.readInt();
    	genre_name = in.readString();
    	viewCount = in.readInt();
    }
	
	public Song(Parcel par) {
		readFromParcel(par);
	}
	
	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		// TODO Auto-generated method stub
		writeToParcel(dest);
	}
	
	public static final Parcelable.Creator<Song> CREATOR = new Parcelable.Creator<Song>() {

		@Override
		public Song createFromParcel(Parcel source) {
			// TODO Auto-generated method stub
			return new Song(source);
		}

		@Override
		public Song[] newArray(int size) {
			// TODO Auto-generated method stub
			return new Song[size];
		}
		
	};
	
	/**
	 * End implement for pass object via aidl
	 */
	
	public int getPriceListenNumber() {
		return Util.getPriceFromString(listenPrice);
	}
	
	public int getPriceDownloadNumber() {
		return Util.getPriceFromString(downloadPrice);
	}
}
